using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class PostTopicDal : Base
    {
        public PostTopicDal()
        {
        }

        public enum Fields
        {
            PostId, 
		    PostName,
		    PostDisplayName,
		    TopicId,
		    TopicName
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.PostId, reader, table);
            GetReaderValue<string>(Fields.PostName, reader, table);
            GetReaderValue<string>(Fields.PostDisplayName, reader, table);
            GetReaderValue<int>(Fields.TopicId, reader, table);
            GetReaderValue<string>(Fields.TopicName, reader, table);
        }

        public Hashtable[] GetAllByPostId(int postId)
        {
            SqlCommand cmd = CreateCommand("GetAllByPostId");
            AddInputParameter(cmd, "PostId", postId, System.Data.SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public void Delete(int postId, int topicId, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "PostId", postId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "TopicId", topicId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, System.Data.SqlDbType.Timestamp);
            base.ExecuteCommand(cmd);
        }

        public void Update(int postId, ref string postName, ref string postDisplayName, int topicId, ref string topicName, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "PostId", postId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "TopicId", topicId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, System.Data.SqlDbType.Timestamp);
            AddOutputParameter(cmd, "PostNameOut", System.Data.SqlDbType.VarChar, 100);
            AddOutputParameter(cmd, "PostDisplayNameOut", System.Data.SqlDbType.VarChar, 100);
            AddOutputParameter(cmd, "TopicNameOut", System.Data.SqlDbType.VarChar, 100);
            AddOutputParameter(cmd, "VersionIdOut", System.Data.SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            postName = GetOutputValue<string>(cmd, "PostNameOut");
            postDisplayName = GetOutputValue<string>(cmd, "PostDisplayNameOut");
            topicName = GetOutputValue<string>(cmd, "TopicNameOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "VersionIdOut");
        }

    }
}
